package cn.com.bluemoon.daps.model.controller.api;


import cn.com.bluemoon.daps.api.model.RemoteDataModelTableFieldService;
import cn.com.bluemoon.daps.common.constant.ServiceNameConstants.DapsApiModel;
import cn.com.bluemoon.daps.common.datascope.BmPermission;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.common.enums.BmStatus;
import cn.com.bluemoon.daps.common.toolkit.BmAssetUtils;
import cn.com.bluemoon.daps.model.entity.DapDataModelTableField;
import cn.com.bluemoon.daps.model.service.DapDataModelTableFieldService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = {"FeignClient服务-模型字段引用标准清除api接口"})
@RequestMapping(DapsApiModel.API_MODEL_FIELD)
@BmPermission(value = "", name = "服务注册-模型字段引用", ignore = true)
@RestController
@Slf4j
public class DapDataModelTableFieldApi implements RemoteDataModelTableFieldService {

    @Resource
    DapDataModelTableFieldService fieldService;

    @PutMapping("/clean")
    public ResultBean<String> cleanRelation(@RequestParam("standardUniqueId")
                                            @ApiParam("标准唯一id") String standardUniqueId) {
        boolean update = fieldService.update(new UpdateWrapper<DapDataModelTableField>()
                .set("standard_id", '0').set("standard_method", null).
                        eq("standard_id", standardUniqueId));
        return update ? ResultBean.ok() : ResultBean.error("关联关系清除失败");
    }


    @PostMapping("/updateField")
    @ApiOperation("变更表字段信息")
    public ResultBean<Boolean> updateSetById(@RequestParam("fieldId") String id,
                                             @RequestBody DapDataModelTableField newT) {
        LambdaUpdateWrapper<DapDataModelTableField> updateWrapper = fieldService.bmLambdaUpdate()
                .set(DapDataModelTableField::getAutoIncrDefaultValue, newT.getAutoIncrDefaultValue())
                .set(DapDataModelTableField::getAutoIncrement, newT.getAutoIncrement())
                .set(DapDataModelTableField::getComment, newT.getComment())
                .set(DapDataModelTableField::getDataType, newT.getDataType())
                .set(DapDataModelTableField::getDefaultValue, newT.getDefaultValue())
                .set(DapDataModelTableField::getFieldName, newT.getFieldName())
                .set(DapDataModelTableField::getLength, newT.getLength())
                .set(DapDataModelTableField::getDecimalLength, newT.getDecimalLength())
                .set(DapDataModelTableField::getNotNull, newT.getNotNull())
                .set(DapDataModelTableField::getPrimaryKey, newT.getPrimaryKey())
                .set(DapDataModelTableField::getFieldRank, newT.getFieldRank())
                .set(DapDataModelTableField::getCompoundType, newT.getCompoundType())
                .set(DapDataModelTableField::getUnSigned, newT.getUnSigned())
                .set(DapDataModelTableField::getZeroFill, newT.getZeroFill())
                .eq(DapDataModelTableField::getId, id);
        boolean status = fieldService.update(updateWrapper);
        log.info("更新表字段，{}, 情况：{}", newT.getModelTableId(), status);
        return ResultBean.ok(status);
    }

    @PostMapping("deleteOrUpdateById")
    @ApiOperation("删除字段或者逻辑删除")
    public ResultBean<Boolean> dropById(@RequestBody DapDataModelTableField oldT,
                                        @RequestParam("del") boolean logicDelete) {
        boolean status;
        if (!logicDelete) {
            status = fieldService.removeById(oldT.getId());
        } else {
            oldT.setBmStatus(BmStatus.DELETE);
            status = fieldService.updateById(oldT);
        }
        log.info("删除字段，{}, 情况：{}", oldT.getModelTableId(), status);
        return ResultBean.ok(status);
    }


    @PostMapping("saveField")
    @ApiOperation("新增字段")
    public ResultBean<String> saveField(@RequestBody DapDataModelTableField newT) {
        //  2021/8/6 重建表：尝试召回历史相同表名中，字段删除的数据 id getRenewField返回的id可能表中已存在！！只能用saveOrUpdate
        newT.setId(fieldService.getRenewField(newT.getModelTableId(), newT.getFieldName()));
        boolean save = fieldService.saveOrUpdate(newT);
        BmAssetUtils.isTrue(save, "保存表字段信息失败！！");
        return ResultBean.ok(newT.getId());
    }
}
